package it.valeandre.omino;

import it.valeandre.omino.utils.Maps;
import it.valeandre.omino.utils.ResBundle;
import it.valeandre.omino.utils.Utils;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Map;

public class EventReceivedActivity extends BannerActivity {

	private TextView mHeader;
	private TextView mTeam1Name;
	private TextView mTeam2Name;
	private TextView mTeam1Score;
	private TextView mTeam2Score;
	private ImageView mTeam1Image;
	private ImageView mTeam2Image;
	private LinearLayout mTeamScoreContainer;
	private TextView mSubHeader;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_result);

		mHeader = (TextView) findViewById(R.id.message_header);
		mSubHeader = (TextView) findViewById(R.id.message_sub_header);
		mTeam1Name = (TextView) findViewById(R.id.message_team_1_name);
		mTeam2Name = (TextView) findViewById(R.id.message_team_2_name);
		mTeam1Score = (TextView) findViewById(R.id.message_team_1_score);
		mTeam2Score = (TextView) findViewById(R.id.message_team_2_score);
		mTeam1Image = (ImageView) findViewById(R.id.message_team_1_logo);
		mTeam2Image = (ImageView) findViewById(R.id.message_team_2_logo);

		mTeamScoreContainer = (LinearLayout) findViewById(R.id.message_score_container);

		Button goToApp = (Button) findViewById(R.id.message_go_to_app_btn);
		goToApp.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(EventReceivedActivity.this, MainActivity.class);
				Bundle bundle = new Bundle();
				bundle.putBoolean(ResBundle.KEY_BUNDLE_FROM_MESSAGE_RECEIVED, true);
				startActivity(intent);
                finish();

			}
		});
		
		Button close = (Button) findViewById(R.id.message_close_btn);
		close.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				finish();
				
			}
		});

		manageIntent(getIntent());
		super.onCreate(savedInstanceState);
		
	}
	
	@Override
	protected void onResume() {
		initBanners();
		super.onResume();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		manageIntent(intent);
		super.onNewIntent(intent);
	}

	private void manageIntent(Intent intent) {
		final String hometeam = intent.getStringExtra("hometeam");
		final String awayteam = intent.getStringExtra("awayteam");
		final String homegoal = intent.getStringExtra("homegoal");
		final String awaygoal = intent.getStringExtra("awaygoal");
		final String details = intent.getStringExtra("goalDetails");
		final String team = intent.getStringExtra("teamGoal");
		
		manageGoal(hometeam, awayteam, homegoal, awaygoal, details, team);
	}
	
	private void manageGoal(String hometeam, String awayteam, String homegoal, String awaygoal, String details, String team) {
		Utils.log("Details = " + details);
		String headerText = "";
		String subHeaderText = "";
		if(team == null || team.trim().equals("")) {
			headerText = getResources().getString(R.string.kickoff);
			mSubHeader.setVisibility(View.GONE);
		} else {
			headerText = getResources().getString(R.string.goal_for);
			headerText += ", " + team;
			
			if (details != null) {
				String[] detailSplit = details.split(":");
				String minute, scorer;
				if(detailSplit.length > 0 && detailSplit[0] != null) {
					minute = detailSplit[0].trim();
				} else {
					minute = "";
				}
				if(detailSplit.length > 1 && detailSplit[1] != null) {
					scorer = detailSplit[1].trim().replace("penalty", getResources().getString(R.string.penalty));
				} else {
					scorer = "";
				}
				
				
				if (scorer != null && !scorer.equals("")) {
					subHeaderText = scorer + ", " + minute;
				} else {
					subHeaderText = minute;
				}
			} else {
				mSubHeader.setVisibility(View.GONE);
			}
		}
		
 		mHeader.setText(headerText);
 		mSubHeader.setText(subHeaderText);
		mTeam1Name.setText(hometeam);
		mTeam2Name.setText(awayteam);
		mTeam1Score.setText(homegoal);
		mTeam2Score.setText(awaygoal);
		Utils.setLogoByName(hometeam, mTeam1Image, Maps.LOGOS_DIMENSION_MID_ID);
        Utils.setLogoByName(awayteam, mTeam2Image, Maps.LOGOS_DIMENSION_MID_ID);
		mTeamScoreContainer.setVisibility(View.VISIBLE);

	}

//	private void setLogoByName(String teamName, ImageView logo) {
//
//		logo.setImageResource(R.drawable.palla_calcio);
//		for(String keyTeam: Maps.TEAM_LOGO_MAP.keySet()) {
//			if(teamName.contains(keyTeam)) {
//				logo.setImageResource(Maps.TEAM_LOGO_MAP.get(keyTeam));
//			}
//		}
//	}

}
